package rabbitmq.direct;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import rabbitmq.Connect;
import rabbitmq.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Productor {
    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = Connect.createConnection();
        // 创建信道
        Channel channel = connection.createChannel();
        // 声明
        channel.exchangeDeclare(Constants.DIRECT_EXCHANGE, BuiltinExchangeType.DIRECT, true);
        channel.queueDeclare(Constants.DIRECT_QUEUE1, false, false, false, null);
        channel.queueDeclare(Constants.DIRECT_QUEUE2, false, false, false, null);
        // 绑定
        channel.queueBind(Constants.DIRECT_QUEUE1, Constants.DIRECT_EXCHANGE, "a");
        channel.queueBind(Constants.DIRECT_QUEUE2, Constants.DIRECT_EXCHANGE, "a");
        channel.queueBind(Constants.DIRECT_QUEUE2, Constants.DIRECT_EXCHANGE, "b");
        channel.queueBind(Constants.DIRECT_QUEUE2, Constants.DIRECT_EXCHANGE, "c");
        // 发送
        String msg_a = "hi rabbitMQ, my routingKey is a";
        channel.basicPublish(Constants.DIRECT_EXCHANGE, "a", null, msg_a.getBytes());
        String msg_b = "hi rabbitMQ, my routingKey is b";
        channel.basicPublish(Constants.DIRECT_EXCHANGE, "b", null, msg_b.getBytes());
        String msg_c = "hi rabbitMQ, my routingKey is c";
        channel.basicPublish(Constants.DIRECT_EXCHANGE, "b", null, msg_c.getBytes());
        // 释放资源
        channel.close();
        connection.close();
    }
}
